var __autocomplete_cache = {};

$(function() {
	$('.autocomplete-holder').each(function() {
		var elm = this, jelm = $(elm);
		jelm.append('<input type="text" class="ac-input" value="" style="border:0;" />');
		var jinput = jelm.find('.ac-input');
		jelm.click(function() {
			jinput.focus();
		});
		$.post(jelm.attr('ac-src-url'), {}, function(r) {
			jinput.data('source', r);
			jinput.autocomplete({
				minLength: 2,
				source: r,
				select: function(ev, ui) {
					$('<a class="ac-item"><input type="hidden" name="'+elm.id+'[]" value="'+ui.item.id+'" />'+ui.item.label+'</a>').insertBefore(jinput);
					jinput.val('');

					return false;
				}
			}).data( "autocomplete" )._renderItem = function( ul, item ) {
				return $( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>" + item.label + "</a>" )
					.appendTo( ul );
			};
		}, 'json');

		jinput.bind('keypress', function(e) {
			if ( e.keyCode == 13 ) {
				if ( $(this).val() ) {
					var vals = $(this).val().split(/(?:\s+)?,(?:\s+)?/);
					$.each(vals, function(i, item) {
						var matcher = new RegExp( $.ui.autocomplete.escapeRegex( item ), "i" );
						var exists_item = $.grep( jinput.data('source'), function( value ) {
							value = value.value 
							return matcher.test( value );
						});

						if ( exists_item.length == 1 ) {
							$('<a class="ac-item"><input type="hidden" name="'+elm.id+'[]" value="'+exists_item[0].id+'" />'+exists_item[0].label+'</a>').insertBefore(jinput);
						} else {
							$('<a class="ac-item"><input type="hidden" name="'+elm.id+'[]" value="'+item+'" />'+item+'</a>').insertBefore(jinput);
						}
					});
					jinput.val('');
				}

				return false;
			}
		});
		$('.autocomplete-holder .ac-item').live('click', function() {
			$(this).remove();
		});
	});

	$('.autocomplete-field').each(function() {
		var elm = this, jelm = $(elm);

		// if use cache
		if ( jelm.attr('ac-use-cache') == 'Y' && typeof(__autocomplete_cache[jelm.attr('ac-src-url')]) != 'undefined') {
			var r = __autocomplete_cache[jelm.attr('ac-src-url')];
			jelm.data('source', r);
			jelm.autocomplete({
				minLength: 2,
				source: r,
				select: function( event, ui ) {
					jelm.val(ui.item.value);

					return false;
				}
			});
		}  else {
			$.post(jelm.attr('ac-src-url'), {}, function(r) {
				jelm.data('source', r);
				jelm.autocomplete({
					minLength: 2,
					source: r,
					select: function( event, ui ) {
						jelm.val(ui.item.value);

						return false;
					}
				});
			}, 'json');
		}
	});
});

function fn_delete_image(r, p)
{
	if (r.deleted == true) {
		$('#' + p.result_ids).replaceWith('<img border="0" src="' + images_dir + '/no_image.gif" />');
		$('a[rev=' + p.result_ids + ']').hide();
	}
}

function fn_delete_image_pair(r, p)
{
	if (r.deleted == true) {
		$('#' + p.result_ids).remove();
	}
}

function fn_check_object_status(obj, status)
{
	if ($(obj).hasClass('cm-active')) {
		$(obj).removeClass('cm-ajax');
		return false;
	}
	fn_update_object_status(obj, status);
	return true;
}
function fn_update_object_status_callback(data, params)
{
	if (data.return_status && params.preload_obj) {
		fn_update_object_status(params.preload_obj, data.return_status.toLowerCase());
	}
}
function fn_update_object_status(obj, status)
{
	var upd_elm_id = $(obj).parents('.cm-popup-box:first').attr('id');
	var upd_elm = $('#' + upd_elm_id);
	upd_elm.hide();
	if ($('input[name=__notify_user]:checked', upd_elm).length) {
		$(obj).attr('href', $(obj).attr('href') + '&notify_user=Y');
	} else {
		$(obj).attr('href', fn_query_remove($(obj).attr('href'), 'notify_user'));
	}
	$('.cm-select-list li a', upd_elm).removeClass('cm-active').addClass('cm-ajax');
	$('.status-link-' + status, upd_elm).addClass('cm-active');
	$('#sw_' + upd_elm_id + ' a').text($('.status-link-' + status, upd_elm).text());

	$('#sw_' + upd_elm_id).removeAttr('class').addClass('selected-status status-' + status + ' ' + $('#sw_' + upd_elm_id + ' a').attr('class'));

}